//
//  ViewController.m
//  放大镜例子
//
//  Created by 杜甲 on 14-3-10.
//  Copyright (c) 2014年 杜甲. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) MagnifierView * fangdaView;
@property (nonatomic,strong) UIImageView * imageView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"福州怡翠花园.jpg"]];
    image.frame  = self.view.bounds;
    image.contentMode = UIViewContentModeScaleAspectFit;
    _imageView = image;
    
    [self.view addSubview:image];
}

//也可以改为pan手势
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if (self.fangdaView == nil) {
        self.fangdaView = [[MagnifierView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        
        self.fangdaView.viewToMagnify = self.imageView;
        //放大的view不能是fangdaView的父视图，否者放大的区域可能包括放大镜自己
        
        [self.view addSubview:self.fangdaView];
        
        self.fangdaView.backgroundColor = [UIColor clearColor];
    }
    self.fangdaView.hidden = NO;
    
//    在刚开始移动时，显示放大镜，传入当前的触摸点
    UITouch* touch = [touches anyObject];
    self.fangdaView.touchPoint = [touch locationInView:self.view];
    [self.fangdaView setNeedsDisplay];
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
//   在移动时，显示放大镜，传入当前的触摸点
	[self handleAction:touches];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//	[self.touchTimer invalidate];
//	self.touchTimer = nil;
    
	self.fangdaView.hidden = YES;
}

- (void)handleAction:(NSSet *)touches {
    
	UITouch *touch = [touches anyObject];
	self.fangdaView.touchPoint = [touch locationInView:self.view];
	[self.fangdaView setNeedsDisplay];
}



@end
